package com.gao.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

//        new Thread(new Runnable()).start();
//        new Thread(new FutureTask<V>()).start();
//        new Thread(new FutureTask<V>(Callable)).start();

//        怎么启动callable呢？
        new Thread().start();
        MyThread myThread = new MyThread();
        FutureTask futureTask = new FutureTask(myThread);

        new Thread(futureTask,"A").start();

        new Thread(futureTask,"B").start();

//        获取Callable的返回值
//        这个get方法可能会产生阻塞，比如在call中耗时过久，所以要放在最后
//        或者直接使用异步通信来处理
        String o = futureTask.get().toString();
        System.out.println(o);
    }
}

class MyThread implements Callable<String> {
    @Override
    public String call() {
        System.out.println("call()");
//        耗时操作
        return "123";
    }
}
